#!/usr/bin/env python
# -*- coding:utf-8 -*-

import re
import time

from task.gsxt.search.exploit.gsxt_shandong_worker import GsxtShanDongWorker


class GsxtDetailShanDongWorker(GsxtShanDongWorker):
    def __init__(self, **kwargs):
        GsxtShanDongWorker.__init__(self, **kwargs)

    def get_token(self, session):
        for _ in xrange(5):
            url = "http://{host}/".format(host=self.host)
            r = self.task_request(session.get, url)
            if r is None:
                self.log.error('访问主页失败了 无法获得token...')
                time.sleep(3)
                continue

            regex = ur"name=\"_csrf\" value=\"(.*?)\""
            search_list = re.search(regex, r.text)
            if search_list is not None:
                return search_list.group(1)
            time.sleep(3)

        self.log.error('没有匹配到 token ')
        return None

    def query_company(self, item):
        try:
            session = self.get_new_session(proxy_type=2)
            token = self.get_token(session)
            if token is None:
                return self.CRAWL_UN_FINISH

            session.headers['X-CSRF-TOKEN'] = token
            seed = item.get('_id', None)

            # 进一步获取详情
            result_length = self.get_detail_html_list(seed, session, item.get('search_list', None))
            if result_length > 0:
                return self.CRAWL_FINISH
        except Exception as e:
            self.log.exception(e)
        return self.CRAWL_UN_FINISH
